package com.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.VO.UserVO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/27 9:17
 */
//@WebServlet("/back/user/update")
public class UserUpdateServlet extends HttpServlet {
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //1、乱码
//        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
//        System.out.println("进入了修改后台");
//        //2、接收前端传过来的值
//        String id = req.getParameter("id");
//        ResultDTO resultDTO=null;
//        try{
//            //3、封装数据
//            UserVO userVO = new UserVO();
//            userVO.setId(Tool.strToInt(id));
//            //4、调用逻辑层
//            UserService userService = new UserServiceImpl();
//            //5、接收逻辑层返回的值
//            resultDTO = userService.findById(userVO);
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        //6、把值返回给前端
//        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
//    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        System.out.println("进入了修改后台");
        //2、接收前端传过来的值
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String birthday = req.getParameter("birthday");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        String enable = req.getParameter("enable");
        //3、封装数据到VO对象
        UserVO userVO = new UserVO();
        userVO.setId(Tool.strToInt(id));
        userVO.setUsername(username);
        userVO.setPassword(password);
        userVO.setPhone(phone);
        userVO.setEmail(email);
        userVO.setBirthday(birthday);
        userVO.setSex(sex);
        userVO.setHobby(hobby);
        userVO.setEnable(Tool.strToInt(enable));
        //4、调用逻辑层
        UserService userService = new UserServiceImpl();
        //5、接收返回值
        ResultDTO result = userService.update(userVO);
        //6、返回给前端
        resp.getWriter().println(JSONObject.toJSONString(result));
    }
}
